import requests
import json
from reqwx import get_access_token
from reqwx import send_module_msg

a_Token = get_access_token()


class User(object):
	def __init__(self, name):
		self.name = name


class UserEncoder(json.JSONEncoder):
	def default(self, o):
		if isinstance(o,User):
			return o.name
		return json.JSONEncoder.default(self, o)



'''
# get accessToken
url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential"
para = {"appid":"wxe7ee3a26be06c699", "secret":"f4dce1c285ee835eb662f02edc1dc094"}
req = requests.get(url,params=para)
# req.content

# get module id
para_m_url = "https://api.weixin.qq.com/cgi-bin/template/get_all_private_template"
req_m_id = requests.post(para_m_url, params={"access_token": a_Token})
#print(req_m_id.content)
module_list = to_json(req_m_id.content)
module_id = module_list["template_list"][0]["template_id"]
print(module_list)
print(module_id)
'''
#get userid  GET

req_get_uid_url = "https://api.weixin.qq.com/cgi-bin/user/get"
para_get_uid_data = {"access_token":a_Token}
res_get_uid = req_get_uid = requests.get(req_get_uid_url, para_get_uid_data)
res_b = req_get_uid.content
res = json.loads(res_b)
openid = res["data"]["openid"][0]

# send msg POST
send_url = "https://api.weixin.qq.com/cgi-bin/message/template/send"
s_a_data = {"access_token": a_Token}

sys_name = "testsystem"
sys_info = "over"

ss_body = {"sysname":{
	"value":sys_name,
	"color":"#203117"
	},
"sysinfo":{
	"value":sys_info,
	"color":"#173177"
}}



txt_body = {'touser':openid,
		"msgtype":"text",
		"text":{
			"content":"Hello,中国"

}}
j_body = json.dumps(txt_body, cls=UserEncoder)
send_user_url = "https://api.weixin.qq.com/cgi-bin/message/custom/send"
r = requests.post(send_user_url,params=s_a_data,json=txt_body)
print(r.content)
'''
#s = send_module_msg(a_Token,openid,module_id,ss_body)

send_url = "https://api.weixin.qq.com/cgi-bin/message/template/send"
access_token = a_Token
para = {"access_token":a_Token}
body = {
	"touser": openid,
	"topcolor": "#FF0000",
	"template_id":module_id,  #t_id,
	"data": {"sysname":{
	"value":sys_name,
	"color":"#203117"
	},
"sysinfo":{
	"value":sys_info,
	"color":"#173177"
}}
}
#j_body = json.dumps(body)

j_body = json.dumps(body)
r = requests.post(send_url,params=para,json=bytes(j_body, encoding="utf8"))

'''